package com.wealth.index.web.controller;

import java.util.List;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.hibernate.service.spi.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.wealth.cart.entity.Cart;
import com.wealth.cart.service.ICartService;
import com.wealth.commodity.entity.Search;
import com.wealth.commodity.service.ICategoryService;
import com.wealth.commodity.service.ISearchService;
import com.wealth.commodity.vo.CategoryVO;
import com.wealth.floor.entity.PcFloor;
import com.wealth.floor.service.IPcFloorService;
import com.wealth.member.service.IMemberService;
import com.wealth.page.entity.Ad;
import com.wealth.page.enums.AdTypeEnum;
import com.wealth.page.service.IADService;
import com.wealth.system.web.controller.BaseController;

import iaas.enums.StatusEnum;
import javassist.bytecode.LineNumberAttribute.Pc;

/**
 * 首页
 * @author XF
 */
@Controller
@RequestMapping(value="/")
@SuppressWarnings(value="all")
public class IndexController extends BaseController {
	
	Logger logger = Logger.getLogger(this.getClass());

	@Autowired
	private ICategoryService categoryService;
	@Autowired
	private IADService adservice;
	@Autowired
	private ICartService cartService;
	@Autowired
	private IPcFloorService floorService;
	@Autowired
	private ISearchService iSearchService;
	@Autowired
	private IMemberService iMemberService;
	/**
	 * 首页
	 * @throws Exception 
	 */
	@RequestMapping(value="",method=RequestMethod.GET)
	public String home(Model model,ServletRequest req, ServletResponse resp) throws Exception{
	
		try {
			//购物车
			if(SecurityUtils.getSubject().isAuthenticated()){
				int count = 0;
				Cart cart=cartService.findCartByMemberIdAndMarketId(memberid, 3L);
				if(cart!=null){
					for (int i = 0; i < cart.getCartItems().size(); i++) {
						count = count + cart.getCartItems().get(i).getAmount();
					}
					model.addAttribute("cart",cart);
					if(cart.getCartItems()!=null){
						model.addAttribute("cartCount", count);
					}
				}
			}
			//广告
			List<Ad> adList = adservice.getAdByCorpIdAndTypeAndStatus(corpid,AdTypeEnum.PCAD.getValue(), StatusEnum.NORMAL.getValue());
			List<Ad> picList = adservice.getAdByCorpIdAndTypeAndStatus(corpid,AdTypeEnum.PCPIC.getValue(), StatusEnum.NORMAL.getValue());
			model.addAttribute("adList",adList);
			model.addAttribute("picList",picList);
			//商品分类
			List<CategoryVO> categoryList = categoryService.getCategoryByStatusAndCorpId(StatusEnum.NORMAL.getValue(), 1L);
			model.addAttribute("categoryList", categoryList);
			/**查询楼层*/
			List<PcFloor> floorList = floorService.getFloorAll();
			model.addAttribute("floorList", floorList);
			/**查询pc端的热搜词*/
			List<Search> searchList = iSearchService.findByPCAndStatus();
			model.addAttribute("searchList", searchList);
			model.addAttribute("member", iMemberService.getMember(memberid));
			return "index/index";
		} catch (ServiceException e) {
			logger.error("[商城PC]首页出错！", e);
			return "system/500";
		}
	}
}
